<?php
include "include/config.inc.php";
$installmentArr = array();
$m = 0;
$today = date("m/d/y");
$loanIdVar = "";
$installmentDate = "";
$selectedParty = 0;
$selectParty = "SELECT a.loanId,b.partyId,c.partyName
                  FROM installment a
	                JOIN loan b ON a.loanId = b.loanId
	                JOIN party c ON b.partyId = c.partyId
	               WHERE a.isReceived = 'N'
	            GROUP BY b.partyId
              ORDER BY loanId";
$selectPartyRes = mysql_query($selectParty);
while($selectPartyRow = mysql_fetch_array($selectPartyRes))
{
  $selectPartyId[$m]   = $selectPartyRow['partyId'];
  $selectPartyName[$m] = $selectPartyRow['partyName'];
  $m++;
}

if(isset($_POST['go']))
{
	$selectedParty = $_REQUEST['partyNameSel'];
	$loanIdVar=($_POST['loanId'] == "")?0:$_POST['loanId'];
  $installmentDate = $_REQUEST['curDateYear']."-".$_REQUEST['curDateMonth']."-".$_REQUEST['curDateDay'];
  $today = $installmentDate;
	if($loanIdVar == 0)
	{
    $i=0;
	  if($_REQUEST['partyNameSel'] == 0)
	  {
 	    $selInstallmentDate = "SELECT a.loanId, a.installmentId,a.loanId,a.installmentNo,a.installmentDate,b.installmentReceived,b.installmentAmount,a.isReceived,b.partyId,c.partyName
	                             FROM installment a
	                             JOIN loan b ON a.loanId = b.loanId
	                             JOIN party c ON b.partyId = c.partyId
	                            WHERE a.installmentDate <= '".$installmentDate."' AND a.isReceived = 'N'
                           ORDER BY loanId,installmentDate";
	  }
	  else
	  {
	  	$partyId = $_REQUEST['partyNameSel'];
	    $selInstallmentDate = "SELECT a.loanId, a.installmentId,a.loanId,a.installmentNo,a.installmentDate,b.installmentReceived,b.installmentAmount,a.isReceived,b.partyId,c.partyName
	                             FROM installment a
	                             JOIN loan b ON a.loanId = b.loanId
	                             JOIN party c ON b.partyId = c.partyId
	                            WHERE a.installmentDate <= '".$installmentDate."' 
	                              AND a.isReceived = 'N'
	                              AND b.partyId = ".$partyId."
                           ORDER BY loanId,installmentDate" ;
	  }
	  $selInstallmentDateRes=mysql_query($selInstallmentDate);
	  while($selInstallmentDateResRow=mysql_fetch_array($selInstallmentDateRes))
	  {
	    $installmentArr[$i]['loanId']              = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentNo']       = $selInstallmentDateResRow['installmentNo'];
	    $installmentArr[$i]['installmentId']       = $selInstallmentDateResRow['installmentId'];
	    $installmentArr[$i]['partyName']           = $selInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanId']              = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentDate']     = date("d-m-Y", strtotime($selInstallmentDateResRow['installmentDate']));
	    $installmentArr[$i]['installmentReceived'] = $selInstallmentDateResRow['installmentReceived'];
	    $installmentArr[$i]['installmentAmount']   = $selInstallmentDateResRow['installmentAmount'];
	    $installmentArr[$i]['isReceived']          = $selInstallmentDateResRow['isReceived'];
	    $i++;
	  }
  }
  else
  {
    $i=0;
	  $installmentDate    = $_REQUEST['curDateYear']."-".$_REQUEST['curDateMonth']."-".$_REQUEST['curDateDay'];
	  $today = $installmentDate;
    $selInstallmentDate = "SELECT a.installmentId,a.loanId,a.installmentNo,a.installmentDate,b.installmentReceived,b.installmentAmount,a.isReceived,b.partyId,c.partyName
	                           FROM installment a
	                           JOIN loan b ON a.loanId = b.loanId
	                           JOIN party c ON b.partyId = c.partyId
	                          WHERE a.installmentDate <= '".$installmentDate."' AND a.isReceived = 'N' AND a.loanId='$loanIdVar'
                         ORDER BY loanId,installmentDate";
	  $selInstallmentDateRes=mysql_query($selInstallmentDate);
	  while($selInstallmentDateResRow=mysql_fetch_array($selInstallmentDateRes))
	  {
	    $installmentArr[$i]['installmentNo']       = $selInstallmentDateResRow['installmentNo'];
	    $installmentArr[$i]['installmentId']       = $selInstallmentDateResRow['installmentId'];
	    $installmentArr[$i]['partyName']           = $selInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanId']              = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentDate']     = date("d-m-Y", strtotime($selInstallmentDateResRow['installmentDate']));
	    $installmentArr[$i]['installmentReceived'] = $selInstallmentDateResRow['installmentReceived'];
	    $installmentArr[$i]['installmentAmount']   = $selInstallmentDateResRow['installmentAmount'];
	    $installmentArr[$i]['isReceived']          = $selInstallmentDateResRow['isReceived'];
	    $i++;
	  }
  }
}

if(isset($_POST['sub']))
{
  echo "<br> A R ".$arrIA=count($_POST['amountReceived']);
  echo "<br> I R ".$installmentValue=$_POST['installmentReceived']."<br>";
  echo "<br> Date ".$installmentDate=$_POST['installmentDate']."<br>";
  for($i=0;$i<$arrIA;$i++)
  {
  	if($_POST['amountReceived'][$i] != "")
  	{
  		echo "<br> Loop ".$countLoop=$_POST['amountReceived'][$i]/$installmentValue=$_POST['installmentReceived'];
  		for($j=0;$j<$countLoop;$j++)
  		{
        $_POST['amountReceived'][$i];    	
        $InstallmentsID = $_POST['InstallmentsID'][$i];
        $InstallmentsIDADD = $InstallmentsID + $j;
        $loanId = $_POST['loanId'][$i];
        echo $updInstallMent="UPDATE `installment` SET  `isReceived` =  'Y', `receiveDate` = '".$installmentDate."'
                          WHERE `installmentId` =".$InstallmentsIDADD." AND loanId=".$loanId;
        mysql_query($updInstallMent);
      }
  	}
  }
}

$smarty->assign("today",$today);
$smarty->assign("selectPartyId",$selectPartyId);
$smarty->assign("selectPartyName",$selectPartyName);
$smarty->assign("selectedParty",$selectedParty);
$smarty->assign("loanIdVar",$loanIdVar);
$smarty->assign("installmentArr",$installmentArr);
$smarty->assign("installmentDate",$installmentDate);
$smarty->display('loanDetail1.tpl');
?>